package net.stogi.dsper.ui.client.module.dataobject;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.gwt.gxt.client.core.util.WidgetUtils;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.common.mvp.core.model.KeyValues;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridGroupRenderer;
import com.extjs.gxt.ui.client.widget.grid.GroupColumnData;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;

class DataObjectListWidget extends Composite implements DataObjectListView {

	private final Grid<ModelData> dataObjectGrid;

	private final GroupingStore<ModelData> dataObjectStore = new GroupingStore<ModelData>();

	private final ContentPanel mainPanel = new ContentPanel();

	public DataObjectListWidget() {
		dataObjectStore.setDefaultSort("userProfileName", SortDir.ASC);
		dataObjectStore.groupBy("userProfileName");

		List<ColumnConfig> columnConfigList = new ArrayList<ColumnConfig>();

		ColumnConfig columnConfig = new ColumnConfig();
		columnConfig.setHeader("Profile");
		columnConfig.setId("userProfileName");
		columnConfig.setWidth(200);
		columnConfigList.add(columnConfig);

		columnConfig = new ColumnConfig();
		columnConfig.setHeader("Title");
		columnConfig.setId("title");
		columnConfig.setWidth(125);
		columnConfigList.add(columnConfig);

		columnConfig = new ColumnConfig();
		columnConfig.setHeader("Published");
		columnConfig.setId("publishedDate");
		columnConfig.setWidth(125);
		columnConfigList.add(columnConfig);

		columnConfig = new ColumnConfig();
		columnConfig.setHeader("Author");
		columnConfig.setId("author");
		columnConfig.setWidth(150);
		columnConfigList.add(columnConfig);

		final ColumnModel columnModel = new ColumnModel(columnConfigList);

		GroupingView groupingView = new GroupingView();
		groupingView.setShowGroupedColumn(false);
		groupingView.setForceFit(true);
		groupingView.setGroupRenderer(new GridGroupRenderer() {

			public String render(GroupColumnData data) {
				String f = columnModel.getColumnById(data.field).getHeader();
				String l = data.models.size() == 1 ? "Item" : "Items";
				return f + ": " + data.group + " (" + data.models.size() + " " + l + ")";
			}
		});

		dataObjectGrid = new Grid<ModelData>(dataObjectStore, columnModel);
		dataObjectGrid.setView(groupingView);

		initComposite();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void appear() {
		mainPanel.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainPanel.hide();
		doClearDataObjectList();
	}

	@Override
	public void displayDataObjects(List<KeyValues<String>> dataObjects) {
		WidgetUtils.populateStoreWithModel(dataObjectStore, dataObjects);
	}

	private void doClearDataObjectList() {
		dataObjectStore.removeAll();
	}

	private void initComposite() {
		dataObjectGrid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		dataObjectGrid.setAutoExpandColumn("title");
		dataObjectGrid.setAutoHeight(true);
		dataObjectGrid.setBorders(true);

		mainPanel.setHeading("Data Object List");
		mainPanel.setFrame(true);
		mainPanel.setVisible(true);
	}

	private void initLayout() {
		mainPanel.add(dataObjectGrid);
	}

	@Override
	public void setRowSelectedBehavior(final BehaviorWithValue<KeyValues<String>> behavior) {
		dataObjectGrid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<ModelData>() {

			@Override
			public void selectionChanged(SelectionChangedEvent<ModelData> se) {
				ModelData selectedItem = se.getSelectedItem();
				if (selectedItem != null) {
					behavior.behave(selectedItem.<KeyValues<String>> get("__bean"));
				}
				else {
					behavior.behave(null);
				}
			}
		});
	}
}
